package com.thinkgem.jeesite.modules.ta.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.thinkgem.jeesite.modules.ta.entity.StWorktime;

public class CanlendarFace {

	public String showHtml(String year, String month, String date,
			List<StWorktime> list) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		String today = format1.format(new Date());
		int day = 0;
		int y, m, d;
		GregorianCalendar temp = new GregorianCalendar();// 取得当前的日历（格里高里历）
		try {
			y = Integer.parseInt(year);
			m = Integer.parseInt(month);
			d = Integer.parseInt(date);
		} catch (Exception e) {
			y = temp.get(GregorianCalendar.YEAR); // 取得年份 ;
			m = temp.get(GregorianCalendar.MONTH); // 取的月份
			d = temp.get(GregorianCalendar.DAY_OF_MONTH); // 取的当前日
		}
		if (y < 1900 || y > 9999)
			y = temp.get(GregorianCalendar.YEAR); // 取得年份 ;
		if (m == -1) {
			y--;
			m = 11;
		} else if (m == 12) {
			y++;
			m = 0;
		} else if (m < -1 || m > 12) {
			m = temp.get(GregorianCalendar.MONTH); // 取的月份
		}
		if (d < 1 || d > 31)
			d = 1;// 默认日
		Calendar thisMonth = Calendar.getInstance();
		thisMonth.set(y, m, d);
		thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
		thisMonth.set(Calendar.DAY_OF_MONTH, 1);
		int firstIndex = thisMonth.get(Calendar.DAY_OF_WEEK) - 1;
		int maxIndex = thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
		String[] days = new String[42];
		for (int i = 0; i < 42; i++) { // 初始化表格数据
			days[i] = "&nbsp;";
		}
		for (int i = 0; i < maxIndex; i++) { // 初始化具有实际日期值得部分
			days[firstIndex + i] = String.valueOf(i + 1);
		}
		String face = "<center><h3>工作日设置</h3></center>";
		face += "<center style='font-size:14px;'>当前日期：<input readonly style='background-color: #FF00FF;width:50px;border:none;' />周末：<input readonly style='background-color: pink;width:50px;border:none;' />该月已设置上班时间的日期：<input readonly style='background-color: #C69BFF;width:50px;border:none;' /></center>";
		face += "<table style='font-size:14px;'  id='t1' width='60%' border='1' align='center' cellpadding='2' cellspacing='1' bordercolordark='#94CCF6' bordercolorlight='#fefefe'>";
		face += "<tr>";
		face += "<td height='35' colspan='7' align='center' bgcolor='#CAE8FF'>";
		face += "<div style='float:left;width: 35%;'><a href='/jeesite/a/ta/stWorktime/upYear?year=" + (y - 1)
				+ "&month=" + m + "&date=1'>上一年</a>";
		face += "<a href='/jeesite/a/ta/stWorktime/upDown?year=" + y
				+ "&month=" + (m - 1) + "&date=1'>上一月</a></div>";
		face += "<div style='float:left;width: 30%;'><strong class='STYLE4'>　" + y + " 年 " + (m + 1)
				+ "  月 </strong></div>";// "+d +" 日";
		face += "<div style='float:left;width: 35%;'><a href='/jeesite/a/ta/stWorktime/downUp?year=" + y
				+ "&month=" + (m + 1) + "&date=1'>下一月</a>";
		face += "&nbsp;<a href='/jeesite/a/ta/stWorktime/downYear?year="
				+ (y + 1) + "&month=" + m + "&date=1'>下一年</a></div>";
		face += "</td></tr>";
		face += "<tr>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'><span class='STYLE3'>日</span></td>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'>一</td>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'>二</td>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'>三</td>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'>四</td>";
		face += "<td width='14%' align='center' bgcolor='#C8E1FA'>五</td>";
		face += "<td align='center' bgcolor='#C8E1FA'><span class='STYLE3'>六</span></td>";
		face += "</tr>";

		for (int j = 0; j < 6; j++) {
			
			face += "<tr id='line" + j + "'>";

			String xdate = "";
			// 星期一到星期五

			for (int i = j * 7; i < (j + 1) * 7; i++) {
				if (i == 35 && days[i].equals("&nbsp;")) {
					face += "<script>line5.style.display='none'</script>";// "<td>&nbsp;</td>";
					break;
				}
				if (days[i].equalsIgnoreCase(String.valueOf(temp
						.get(GregorianCalendar.DAY_OF_MONTH)))
						&& y == temp.get(GregorianCalendar.YEAR)
						&& m == temp.get(GregorianCalendar.MONTH)) {
					xdate = y + "-" + (m + 1) + "-" + days[i];
					Date dayDate;
					try {
						dayDate = format1.parse(xdate);
						xdate = format1.format(dayDate);
					} catch (ParseException e) {
						day = 0;
					}
					day = 0;
					for (StWorktime stWorktime : list) {
						String dateDo = format1.format(stWorktime.getSdate());
						if (dateDo.equals(xdate)) {
							day = 1;
						}
					}
					if (day == 1) {
						face += "<td bgcolor='#C69BFF' height='50' align='center' <span class='STYLE10'>";
					} else {
						face += "<td bgcolor='#FF00FF' height='50' align='center' <span class='STYLE10'>";
					}
				} else if ((i % 7) == 0 || ((i + 1) % 7) == 0) {
					// 星期六星期天
					if (days[i] != "&nbsp;") {
						xdate = y + "-" + (m + 1) + "-" + days[i];
						Date dayDate;
						try {
							dayDate = format1.parse(xdate);
							xdate = format1.format(dayDate);
						} catch (ParseException e) {
							day = 0;
						}
						day = 0;
						for (StWorktime stWorktime : list) {
							String dateDo = format1.format(stWorktime
									.getSdate());
							if (dateDo.equals(xdate)) {
								day = 1;
							}
						}
						if (day == 1) {
							face += "<td height='50' align='center' <span bgcolor='#C69BFF' style='font-weight:bold;' class='STYLE6'>";
						} else {
							face += "<td height='50' align='center' <span bgcolor='pink' style='font-weight:bold;' class='STYLE6'>";
						}
					} else {
						face += "<td height='50' align='center' <span style='font-weight:bold;' class='STYLE6'>";
					}
				} else {
					xdate = y + "-" + (m + 1) + "-" + days[i];
					Date dayDate;
					try {
						dayDate = format1.parse(xdate);
						xdate = format1.format(dayDate);
					} catch (ParseException e) {
						day = 0;
					}
					day = 0;
					for (StWorktime stWorktime : list) {
						String dateDo = format1.format(stWorktime.getSdate());
						if (dateDo.equals(xdate)) {
							day = 1;
						}
					}
					if (day == 1) {
						face += "<td height='50' align='center' <span bgcolor='#C69BFF' style='font-weight:bold;' class='STYLE6'>";
					} else {
						face += "<td height ='50' align='center'  <span style='font-weight:bold;' class='STYLE7'>";
					}
				}

				if (days[i] != "&nbsp;") {
					xdate = y + "-" + (m + 1) + "-" + days[i];
					Date dayDate;
					try {
						dayDate = format1.parse(xdate);
						xdate = format1.format(dayDate);
					} catch (ParseException e) {
					}
					if (today.equals(xdate)) {
						face += "<a href='javascript:show(\"" + xdate + "\","
								+ y + "," + m + "," + days[i] + ")' style='color:#fff'>" + days[i]
								+ "</a></span>";
					} else {
						face += "<a href='javascript:show(\"" + xdate + "\","
								+ y + "," + m + "," + days[i] + ")'>" + days[i]
								+ "</a></span>";
					}
				} else {
					face += days[i] + "</span>";
				}
				face += "</td>";
			}
			face += "</tr>";
		}
		face += "</table>";
		return face;
	}
}
